package com.qf.service.impl;

import com.qf.dao.OrderDao;
import com.qf.dao.impl.OrderDaoImpl;
import com.qf.domain.*;
import com.qf.service.*;
import com.qf.utils.DataSourceUtil;

import java.sql.SQLException;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao=new OrderDaoImpl();
    @Override
    public void submitOrder(Order order, List<Cart> cartList) {
        try {
            DataSourceUtil.begin();
            orderDao.add(order);
            for (Cart cart : cartList) {
                OrderDetail orderDetail=new OrderDetail(0,order.getId(),cart.getPid(),cart.getNum(),cart.getMoney());
                orderDao.addDetail(orderDetail);
            }

            CartService cartService=new CartServiceImpl();
            cartService.clear(order.getUid());
            DataSourceUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DataSourceUtil.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            throw  new RuntimeException(e);
        } finally {
            try {
                DataSourceUtil.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Order> find(Integer uid ) {
       List<Order>orderList= orderDao.select(uid);
        if (orderList != null&&orderList.size()>0) {
            AddressService addressService=new AddressServiceImpl();
            List<Address> addressList = addressService.find(uid);
            if(addressList!=null){
                for (Order order : orderList) {
                    for (Address address : addressList) {
                        if(order.getAid()==address.getId()){
                            order.setAddress(address);
                        }
                    }
                }
            }

        }
        return orderList;
    }

    @Override
    public Order findByOid(String oid) {
       Order order= orderDao.selectByid(oid);
       if(order!=null){
           AddressService addressService=new AddressServiceImpl();
           List<Address> addresses = addressService.find(order.getUid());
           if (addresses != null) {
               for (Address address : addresses) {
                   if(address.getId()==order.getAid()){
                        order.setAddress(address);
                   }
               }
           }
       }
        return order;
    }

    @Override
    public List<OrderDetail> findDetail(String oid) {
        List<OrderDetail> orderDetails=orderDao.selectDetail(oid);
        if (orderDetails != null||orderDetails.size()>=0) {
            GoodsService goodsService=new GoodsServiceImpl();
            for (OrderDetail orderDetail : orderDetails) {
                Goods goods=goodsService.findById(orderDetail.getPid());
                orderDetail.setGoods(goods);
            }
        }
        return orderDetails;
    }

    @Override
    public List<Order> findAll() {
        List<Order> orderList = orderDao.selectAll();
        if(orderList!=null&&!orderList.isEmpty()){
            for (Order order : orderList) {
                UserService userService=new UserServiceImpl();
                User user = userService.findById(order.getUid());
                order.setUser(user);
            }
        }
        return orderList;
    }

    @Override
    public List<Order> fAll(String where, List<Object> params) {
        List<Order> orderList = orderDao.sAll(where,params);
        if(orderList!=null&&!orderList.isEmpty()){
            for (Order order : orderList) {
                UserService userService=new UserServiceImpl();
                User user = userService.findById(order.getUid());
                order.setUser(user);
            }
        }
        return orderList;
        //return orderDao.sAll(where,params);
    }


}

